package com.wx.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wx.model.Users;
import com.wx.service.UserService;
import com.wx.serviceImpl.UserServiceImpl;

/**
 * Servlet implementation class UserLoginServlet
 */
@WebServlet("/servlet/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String usernamelogin = request.getParameter("phoneno");
		String passwordlogin = request.getParameter("password");
		UserService userservicelogin=new UserServiceImpl();
		Users userlogin=new Users();
		userlogin.setUsername(usernamelogin);
		userlogin.setPassword(passwordlogin);
		try {
			Users login = userservicelogin.login(userlogin);
			if(login.getUsername()==null){
				response.sendRedirect("../login.jsp");
			}else{
				HttpSession session = request.getSession();
				session.setAttribute("login", login);
				request.getRequestDispatcher("../index.jsp").forward(request, response);
				
				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
	}

}
